package com.wanxi.app.service.app;

import com.wanxi.app.domain.dto.SortDto;
import com.wanxi.app.domain.result.AppResult;
import com.wanxi.app.domain.vo.AppProductVo;
import com.wanxi.app.domain.vo.AppTypeVo;
import com.wanxi.app.domain.vo.IndexVo;
import com.wanxi.app.domain.vo.SaleProduct;
import com.wanxi.app.utils.BeanCopyUtils;
import com.wanxi.product.service.api.feign.ProductUserService;
import com.wanxi.product.service.api.query.shop.SkuQueryShop;
import com.wanxi.product.service.api.query.user.SkuQuery;
import com.wanxi.product.service.api.vo.user.SkuFindAllOutVo;
import com.wanxi.product.service.api.vo.user.SkuFindByIdOutVo;
import com.wanxi.product.service.api.vo.user.TypeFindAllOutVo;
import com.wanxi.repertory.service.api.dto.RepertoryUserPageDto;
import com.wanxi.repertory.service.api.entity.RepertoryUser;
import com.wanxi.repertory.service.api.feign.RepertoryUserService;
import com.wanxi.shop.api.feign.ShopFeignClient;
import com.wanxi.shop.api.result.CommonResult;
import com.wanxi.shop.api.vo.FindShopAll;
import com.wanxi.shop.api.vo.FindShopNameOutVO;
import com.wanxi.shop.api.vo.SkuFindByShopOutVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author:wgy
 * @Data：2022/1/14 16:26
 */
@Service
public class AppService {

    @Resource
    private ProductUserService productUserService;

    @Resource
    private ShopFeignClient shoppingCartFeign;

    @Resource
    private RepertoryUserService repertoryUserService;

    public CommonResult findShopNameById(Long id){
        CommonResult shopNameById = shoppingCartFeign.findShopNameById(id);
        return shopNameById;
    }

    /**
     *
     * @param sortDto 搜索条件
     * @return
     */
    public AppResult<Object> findProduct(SortDto sortDto){
        if (sortDto.getSort()>0){
            SkuQuery build = SkuQuery.builder()
                    .latitude(sortDto.getLatitude())
                    .longitude(sortDto.getLongitude())
                    .data(sortDto.getData())
                    .build();
            List<SkuFindAllOutVo> SkuFindAllOutVoList = productUserService.findAllSku(build).getData();
            List<AppProductVo> appProductVos = new ArrayList<>();
            for (SkuFindAllOutVo item: SkuFindAllOutVoList
                 ) {
                Long id = 1L;
//                Long id = item.getSkuId();
                String data = (String)shoppingCartFeign.findShopNameById(id).getData();
                AppProductVo appProductVo = AppProductVo.builder()
                        .shopId(id)
                        .img(item.getImg())
                        .shopName(data)
                        .skuId(item.getSkuId())
                        .salePrice(item.getSalePrice())
                        .skuName(item.getSkuName())
                        .build();
                appProductVos.add(appProductVo);
            }
            return AppResult.success(appProductVos);
        }
        else {
            String shopName = sortDto.getData();
            CommonResult<List<FindShopNameOutVO>> shopName1 = shoppingCartFeign.findShopName(shopName);
            return AppResult.success(shopName1.getData());
        }

    }

    /**
     * 获取c端首页信息
     * @return
     */
    public AppResult<IndexVo> index() {

        IndexVo index = new IndexVo();
        RepertoryUserPageDto build = RepertoryUserPageDto.builder()
                .page(0)
                .pageSize(4)
                .build();

//        获取热销商品
        List<RepertoryUser> list = repertoryUserService.findRepertory(build).getData().getList();
        List<SaleProduct> saleProductList = new ArrayList<>();
        if (list!=null){
            for (RepertoryUser item: list) {
                SkuFindByIdOutVo skuFindByIdOutVo = productUserService.findSkuById(item.getSkuId()).getData();
                CommonResult<String> shopNameById = shoppingCartFeign.findShopNameById(item.getShopId());

                SaleProduct copy = SaleProduct.builder()
                        .skuId(skuFindByIdOutVo.getSkuId())
                        .skuName(skuFindByIdOutVo.getSkuName())
                        .img(skuFindByIdOutVo.getImg())
                        .build();
                copy.setShopName(shopNameById.getData());
                copy.setSaleTotal(item.getSaleTotal());
                copy.setShopId(item.getShopId());
                saleProductList.add(copy);
            }
        }
        index.setSaleProductList(saleProductList);
//      获取分类

        List<TypeFindAllOutVo> data = productUserService.findAllType(0).getData();
        List<AppTypeVo> appTypeVoList = BeanCopyUtils.copyList(data, AppTypeVo.class);
        index.setTypeList(appTypeVoList);
        return AppResult.success(index);
    }


    /**
     * 点击商品查看商品详情
     * @param skuId
     * @return
     */
    public com.wanxi.product.service.api.result.CommonResult<SkuFindByIdOutVo> findSkuBySkuId(Long skuId) {
        com.wanxi.product.service.api.result.CommonResult<SkuFindByIdOutVo> skuById = productUserService.findSkuById(skuId);
        com.wanxi.repertory.service.api.result.CommonResult<Long> shop = repertoryUserService.findShopIdBySkuId(skuId);
        skuById.getData().setShopId(shop.getData());
        return skuById;
    }


    public com.wanxi.shop.api.result.CommonResult<List<com.wanxi.shop.api.vo.SkuFindAllOutVo>> findSkuByType(Long typeId) {
        return shoppingCartFeign.findSkuByType(typeId);
    }

    public CommonResult<List<SkuFindByShopOutVo>> findSkuByShopId(Long shopId) {
        return shoppingCartFeign.findSkuByShopId(shopId);
    }

    public CommonResult<List<FindShopAll>> findShopAll(Long shopId) {
        return shoppingCartFeign.findAllShop(shopId);
    }
}
